package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUiPage;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.HttpClientUtil;
import com.taotao.common.util.IDUtils;
import com.taotao.common.util.JsonUtils;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.service.ItemService;

@Service("itemService")
public class ItemServiceImpl implements ItemService {
	@Value("${SEARCH_BASE_URL}")
	private String SEARCH_BASE_URL;
	@Autowired
	TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;

	@Override
	public TbItem findItemById(Long itemId) {
		return itemMapper.selectByPrimaryKey(itemId);
	}

	@Override
	public EasyUiPage findItemList(int page, int rows) {
		PageHelper.startPage(page, rows);
		TbItemExample example = new TbItemExample();
		List<TbItem> list = itemMapper.selectByExample(example);
		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
		return new EasyUiPage(pageInfo.getTotal(), list);
	}

	@Override
	public TaotaoResult createItem(TbItem item, String desc, String itemParams) {
		long itemId = IDUtils.genItemId();
		item.setId(itemId);
		item.setStatus((byte) 1);
		Date currDate = new Date();
		item.setCreated(currDate);
		item.setUpdated(currDate);
		itemMapper.insert(item);
		TaotaoResult result = createItemDesc(itemId, desc, currDate);
		if (200 != result.getStatus()) {
			throw new RuntimeException("插入商品描述表异常!");
		}
		TaotaoResult result1 = createItemParam(itemId, itemParams);
		if (200 != result1.getStatus()) {
			throw new RuntimeException("插入商品参数表异常!");
		}

		return TaotaoResult.ok(item);
	}

	private TaotaoResult createItemDesc(Long itemId, String desc, Date currDate) {
		TbItemDesc itemDesc = new TbItemDesc();
		itemDesc.setItemId(itemId);
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(currDate);
		itemDesc.setUpdated(currDate);
		itemDescMapper.insert(itemDesc);
		return TaotaoResult.ok();
	}

	private TaotaoResult createItemParam(Long itemId, String itemParams) {
		TbItemParamItem itemParamItem = new TbItemParamItem();
		itemParamItem.setItemId(itemId);
		itemParamItem.setParamData(itemParams);
		Date date = new Date();
		itemParamItem.setCreated(date);
		itemParamItem.setUpdated(date);
		itemParamItemMapper.insert(itemParamItem);
		return TaotaoResult.ok();

	}

	@Override
	public TaotaoResult addSearchIndex(TbItem item) {
		// 3张表的数据都插入成功后,需要添加索引,需要通过httpClient调用
		try {
			String url = SEARCH_BASE_URL + "/item/importOne?id=" + item.getId();
			String json = HttpClientUtil.doGet(url);
			TaotaoResult result2 = JsonUtils.toObject(json, TaotaoResult.class);
			if (200 != result2.getStatus())
				throw new RuntimeException("往solr服务器添加索引失败");
		} catch (Exception e) {
			throw new RuntimeException("往solr服务器添加索引失败");
		}
		return TaotaoResult.ok();
	}

}
